package com.atguigu.cms.controller;


import com.atguigu.cms.entity.CrmBanner;
import com.atguigu.cms.service.CrmBannerService;
import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-03
 */
@RestController
@CrossOrigin
@RequestMapping("/educms/banneradmin")
public class BannerAdminController {
    CrmBannerService crmBannerService;
    @Autowired
    public void setCrmBannerService(CrmBannerService crmBannerService) {
        this.crmBannerService = crmBannerService;
    }

    @GetMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page,@PathVariable long limit){
        Page<CrmBanner> bannerPage = new Page<>(page,limit);
        crmBannerService.page(bannerPage,null);
        return R.ok().data("items",bannerPage.getRecords()).data("total",bannerPage.getTotal());
    }

    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        if(crmBannerService.save(crmBanner)) {
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        if(crmBannerService.updateById(crmBanner)){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @DeleteMapping("/{bannerId}")
    public R deleteBanner(@PathVariable String bannerId){
        if(crmBannerService.removeById(bannerId)){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

